package com.alibaba.citrus.cr.mn.order.center.facade.function.common;

import cn.hutool.core.date.StopWatch;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.mnordercenter.orderbaserequest.OrderBizUpdateResponse;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangxiaorui
 * @ClassName OrderBizCreateService
 * @description: TODO
 * @date 2023/05/16 11:28
 * @version: 1.0
 */
public interface OrderBizUpdateService<BizInput, AtomInput, BizOutput> {

    Log log = Log.getLogger(OrderBizUpdateService.class);

    default BizOutput update(BizInput orderBizUpdateRequest) {
        try {
            StopWatch stopWatch = new StopWatch("OrderBizUpdateService");

            // 1.0 非空，长度，枚举校验
            stopWatch.start("paramsInvalidCheck");
            paramsInvalidCheck(orderBizUpdateRequest);
            stopWatch.stop();

            // 2.0 参数预处理，订单头公共属性 同步更新至 订单明细
            stopWatch.start("preHandleRequestParams");
            preHandleRequestParams(orderBizUpdateRequest);
            stopWatch.stop();

            // 3.0 业务校验-防腐适配处理 以及基础数据转换
            stopWatch.start("assembleBizBaseInfo");
            BizOutput responses = assembleBizBaseInfo(orderBizUpdateRequest);
            stopWatch.stop();

            // 4.0 商业Request->原子Request
            stopWatch.start("convertAtomRequest");
            AtomInput atomUpdateRequest = convertAtomRequest(orderBizUpdateRequest);
            stopWatch.stop();

            // 5.0 调用订单原子创建能力
            stopWatch.start("processAtomAbility");
            BizOutput bizOutput = processAtomAbility(atomUpdateRequest, responses);
            stopWatch.stop();

            log.info("OrderBizUpdateService#update cost time:{}", stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
            return bizOutput;
        } catch (Exception e) {
            log.error("OrderBizUpdateService#update error", e);
            throw e;
        }
    }

    void preHandleRequestParams(BizInput bizBatchCreateRequest);

    void paramsInvalidCheck(BizInput bizBatchUpdateRequest);

    BizOutput assembleBizBaseInfo(BizInput bizOrderCreateList);

    BizOutput assembleBizBaseInfoNew(BizInput bizOrderCreateList, Map<String, Object> queryResponseMap);

    AtomInput convertAtomRequest(BizInput bizBatchCreateRequest);

    BizOutput processAtomAbility(AtomInput atomCreateRequest, BizOutput responses);

    default BizOutput updateWithBeforeDelivery(BizInput request) {
        throw new RuntimeException("此方法未被实现");
    }
}
